# -- coding:utf-8 --

import wiringpi as gpio
from wiringpi import GPIO
from PyQt4 import QtGui, QtCore

class screw(QtCore.QThread):
    
    FinshSignal = QtCore.pyqtSignal()

    def __init__(self, parent = None):
        super(screw, self).__init__(parent)
        self.PULPort = 13
        self.DIRPort = 6
        self.DelayTime = 10
        gpio.wiringPiSetupGpio()
        gpio.pinMode(self.PULPort, GPIO.OUTPUT)
        gpio.pinMode(self.DIRPort, GPIO.OUTPUT)
        
        self.Length = 0

    def run(self):
        self.Movemm(self.Length)
        self.Length = 0
        self.FinshSignal.emit()

    def Movemm(self, Length):
        if Length > 0:
            gpio.digitalWrite(self.DIRPort, GPIO.HIGH)
        elif Length < 0:
            gpio.digitalWrite(self.DIRPort, GPIO.LOW)

        PULNum = abs(Length) * 4000
        for i in range(PULNum):
            gpio.digitalWrite(self.PULPort, GPIO.HIGH)
            gpio.delayMicroseconds(self.DelayTime)
            gpio.digitalWrite(self.PULPort, GPIO.LOW)
            gpio.delayMicroseconds(self.DelayTime)

if __name__ == "__main__":
    screw = screw()
    screw.Length = -1000
    screw.run()